using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Threading;
using Test;
using System.Text.RegularExpressions;
using System.Data;
namespace EvLogAdd {
	static class EvLogAdd {
		public static DAL local=new DAL();
		static void MainX() {


			//double i=Math.Ceiling(((double)4/((double)3-(double)3))*100d);

			List<string> MachineNames=new List<string>();
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpct{0}","51-75"));

			//PWMS
			MachineNames.AddRange(Mixer.CombineEx("pon-wpct{0:00}","1-30"));
			MachineNames.AddRange(Mixer.CombineEx("pon-wpwb02-cl{0:00}","1,2"));
			MachineNames.AddRange(Mixer.CombineEx("pon-wprf0{0}-cl0{1}","3,6","1,2"));

			//MachineNames.AddRange(Mixer.Combine("pon-wpas10-cl0{0}","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wprf{0:00}-cl0{1}","1-3,5-6","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wprf0{0}-cl0{1}","1","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wprf0{0}-cl0{1}","6","1,2")); 
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpwb{0:00}-cl{1:00}","2","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpas10-cl{0:00}","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wprf{0:00}-cl{1:00}","1","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wpas10-cl0{0}","1,2"));
			//MachineNames.Add("pon-wpsq21-db03");
			//MachineNames.AddRange(Mixer.Combine("pon-wpsq{0}-db","01,06,07,08,09,12,13,16"));

			//MachineNames.AddRange(Mixer.Combine("pon-wpmq01-cl0{0}","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpsq21-cl{0:00}","1-6"));
			//MachineNames.Add("PON-WPRF04-CL01");
			//MachineNames.Add("PON-WPRF04-CL02");
			//MachineNames.Add("PON-WPSQ09-CL01");
			//MachineNames.Add("PON-WPSQ09-CL02");
			//MachineNames.Add("PON-WPSQ03-CL01");
			//MachineNames.Add("pon-wpsq21-db03");
			//MachineNames.Add("pon-wpsq08-db");
			local.NonQry("truncate table elog");
			List<Thread> thLst=new List<Thread>();
			foreach(string machine in MachineNames)
				thLst.Add(new Thread(new LogReader(machine).Read));
			Stopwatch sw=Stopwatch.StartNew();
			foreach(Thread th in thLst)
				th.Start();
			foreach(Thread th in thLst)
				th.Join();
			Console.WriteLine("THE END");
			Console.WriteLine(sw.Elapsed);
		}
	}
	class LogReader {

		string _machine;
		public LogReader(string machine) { _machine=machine; }
		public void Read() {
			for(int j=0;j<1000;j++)
			{
				EventLog[] logs=new EventLog[0];
				try
				{
					logs=EventLog.GetEventLogs(_machine);
				}
				catch(Exception ex) { Console.WriteLine(ex.Message+" - "+_machine); }
				foreach(EventLog log in logs)
					if(log.Log.ToLower()=="application")
					{
						
						//if(string.Compare("application",log.Log,true)==0)
						//if(log.Log.ToLower()!="security")
						for(int i=log.Entries.Count-1;i>=0;i--)
						{
							try
							{
								EventLogEntry ent=log.Entries[i];
								Thread.Sleep(500);
								//if(ent.TimeWritten.Date.AddDays(1)<DateTime.Now.Date)
								if(ent.TimeWritten<DateTime.Now.AddMinutes(-1))
									//if(ent.TimeWritten<DateTime.Now.AddDays(-1))
									break;

								int cnt=(int)EvLogAdd.local.Scalar("select count(*)from elog where machine='{0}' and data='{1}'",ent.MachineName,ent.Index);
								if(cnt==0)
								{
									EvLogAdd.local.NonQry("insert into elog(cat,eType,machine,src,eTime,usr,msg,log,data)values('{0}','{1}','{2}','{3}',convert(datetime,'{4}',120),'{5}','{6}','{7}','{8}')",ent.Category,ent.EntryType,ent.MachineName,ent.Source,ent.TimeGenerated.ToString("yyyy-MM-dd HH:mm:ss"),ent.UserName,ent.Message.Replace("'","''"),log.Log.ToLower(),ent.Index);
									Console.WriteLine(_machine+" - "+log.Entries.Count);
									log.Close();
									//break;
								}
							}
							catch(Exception ex) { Console.WriteLine(ex.Message); break; }

						}




						
					}

			}

			//refresher

		}
	}
}
/*
drop table elog
create table elog(
id int identity,
cat varchar(255),
eType varchar(25),
machine varchar(50),
log varchar(30),
src varchar(255),
eTime datetime,
usr varchar(50),
msg varchar(max),
constraint pk_elog_id primary key(id)
)
create index ix_elog_eTime on elog(eTime)
*/
